package com.joymart.compute.repository;

import com.joymart.common.data.jdbc.BaseRepository;
import com.joymart.compute.model.OrderGain;
import com.joymart.compute.model.AmountAggregation;

import com.joymart.compute.model.PvAggregation;
import com.joymart.compute.model.PvSumWithOwnerId;
import com.joymart.order.model.Order;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;


import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface OrderGainRepository extends BaseRepository<OrderGain,String> {

    @Query("select count(*) as order_count, sum(amount) as pv_sum  from order_gain where owner_id = :ownerId " +
            " and type = :type" +
            " and created_at between :start and :end")
    PvAggregation aggregateOrderGain(@Param("ownerId") String ownerId,
                                     @Param("type") OrderGain.Type type,
                                     @Param("start") LocalDateTime start,
                                     @Param("end") LocalDateTime end);


    @Query("select owner_id, sum(amount) as pv_sum  from order_gain where " +
            " type = :type" +
            " and created_at between :start and :end group by owner_id")
    List<PvSumWithOwnerId> aggregateOrderPvGainOfAll(@Param("type") OrderGain.Type type,
                                              @Param("start") LocalDateTime start,
                                              @Param("end") LocalDateTime end);


    List<OrderGain> findAllByOrderId(String orderId);

    @Query(" select count(*) as order_count, " +
            " sum(first_seller_profit) as amount  from \"order\" " +
            " where id in( :orderIds) " +
            " and status != 'CANCELLED'")
    AmountAggregation aggregateFirstSellerProfit(@Param("orderIds") List<String> orderIds);

    @Query(" select count(*) as order_count, " +
            " sum(second_seller_profit) as amount  from \"order\" " +
            " where id in( :orderIds) " +
            " and status != 'CANCELLED'")
    AmountAggregation aggregateSecondSellerProfit(@Param("orderIds") List<String> orderIds);
}
